import { Button, message } from 'antd';
import { WeaDialog, WeaNewScroll, WeaLocaleProvider } from 'ecCom';
import CarConstant from '../../../util/CarConstant';
import CarInfo from './CarInfo';
import { inject, observer } from 'mobx-react';
import CreateShareScope from './CreateShareScope';
import * as React from 'react';

const getLabel = WeaLocaleProvider.getLabel;
let subid = 0;
let subName = '';


@observer
class CarCard extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      carFormData: undefined,
    };
  }

  /**
   * 加载表单数据
   */
  getCarInfoData = (id) => {
    let { carInfoStore, carid } = this.props;
    if (id && id > 0) {
      carid = id;
    }
    if (carid && carid > 0) {
      carInfoStore.getCarInfo({ id: carid }, this.setCarInfoData.bind(this));
    }
  };

  setCarInfoData = (data) => {
    const { carInfoStore } = this.props;
    const { form } = carInfoStore;
    form.updateFields({
      carNo: {
        value: data.carNo,
      },
      subCompanyId: {
        value: data.subCompanyId,
        valueSpan: data.subCompanyIdName,
        valueObj: [
          {
            id: data.subCompanyId,
            name: data.subCompanyIdName,
          },
        ],
      },
      carType: {
        value: data.carType,
      },
      usefee: {
        value: data.usefee,
      },
      factoryNo: {
        value: data.factoryNo,
      },
      price: {
        value: data.price,
      },
      buyDate: {
        value: data.buyDate,
      },
      engineNo: {
        value: data.engineNo,
      },
      driver: {
        value: data.driver,
        valueSpan: data.driverName,
        valueObj: [
          {
            id: data.driver,
            name: data.driverName,
          },
        ],
      },
      remark: {
        value: data.remark,
      },
      deposit: {
        value: data.deposit,
      },
      showorder: {
        value: data.showorder,
      },
    });
  };

  saveCarInfo = (callback) => {
    const { carInfoStore, carid } = this.props;
    const { form, conditioninfo } = carInfoStore;
    const data = form.getFormParams();
    let f = true;
    for (let i = 0; i < conditioninfo.length; i++) {
      const c = conditioninfo[i];
      const fields = c.items;
      for (let j = 0; j < fields.length; j++) {
        const viewAttr = fields[j].viewAttr;
        const fieldname = fields[j].domkey[0];
        const label = fields[j].label;
        let val = data[fieldname];
        if (fieldname == 'subCompanyId' && val == 0) {
          val = '';
        }
        if (viewAttr == 3 && (val == undefined || `${val}` == '')) {
          f = false;
          message.error(getLabel(384148, '必填项不能为空!'));
          break;
        }
      }
      if (!f) {
        break;
      }
    }
    if (f) {
      const param = { operation: !isNaN(carid) && carid > 0 ? 'edit' : 'add', id: carid, ...data };
      carInfoStore.saveorupdateCar(param, callback);
    }
  };

  clear = () => {
    const { carInfoStore } = this.props;
    const { form, conditioninfo } = carInfoStore;
    form.reset();
    form.updateFields({
      showorder: {
        value: 1,
      } });
  };

  onChange(data) {

  }

  render() {
    const { carInfoStore, carid } = this.props;
    const { form, conditioninfo } = carInfoStore;
    this.getCarInfoData(carid);
    return (
      <div id="carEdit" className="car-form-warp">
        <CarInfo
          ecId={`${this && this.props && this.props.ecId || ''}_CarInfo@ujlks5`}
          form={form}
          carid={carid}
          {...this.props}
          conditioninfo={conditioninfo}
          onChange={this.onChange.bind(this)}
        />
      </div>
    );
  }
}


let isRefreshList = false;

@inject('carInfoStore', 'carManagerStore')
class CarInfoDialog extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      tabKey: 'info',
      itemsVO: props.itemsVO,
      carid: '',
      dialogHeight: document.documentElement.clientHeight,
    }
    ;
    const { carInfoStore } = this.props;
    carInfoStore.initCarInfoForm({}, this.getCarInfoData);
  }

  componentDidMount() {
    window.onresize = this.resize;
  }

  resize = () => {
    this.setState({
      dialogHeight: document.documentElement.clientHeight,
    });
  };

  render() {
    const { visible, itemsVO, carid, dialogHeight } = this.state;
    const dialogH = dialogHeight > 784 ? 522 : Math.max(dialogHeight * 0.670, 380);
    return (
      <WeaDialog
        ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@xcbcv0`}
        className="car"
        style={{ minHeight: dialogH, width: 800 }}
        visible={visible}
        title={itemsVO ? getLabel(385833, '编辑车辆') : getLabel(20317, '新建车辆')}
        icon={CarConstant.iconClassName}
        iconBgcolor={CarConstant.iconBgColor}
        onCancel={this.hideDialog.bind(this)}
        buttons={this.getBtns()}
      >
        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@4as72j`} ref="sc" height={dialogH}>
          <div className="car-dialog-form">
            <CarCard ecId={`${this && this.props && this.props.ecId || ''}_CarCard@1tb08d`} ref="carCard" {...this.props} carid={carid} itemsV={itemsVO} />
          </div>
        </WeaNewScroll>
        <CreateShareScope ecId={`${this && this.props && this.props.ecId || ''}_CreateShareScope@e8vors`} />
      </WeaDialog>
    );
  }

  getBtns = () => {
    const { carInfoStore } = this.props;
    const { isshare } = carInfoStore;
    const { itemsVO } = this.state;
    const btns = new Array();
    btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7ijtcd@submit`} type="primary" onClick={() => this.submit()}>{getLabel(86, '保存')}</Button>);
    if (!itemsVO) { // 如果是新建车辆
      if (isshare == 1) { // 并且开启了车辆共享
        btns.push(<Button
          ecId={`${this && this.props && this.props.ecId || ''}_Button@8rpeeq@submitAndSet`}
          type="primary"
          onClick={() => this.submitAndSet()}
        >{getLabel(32159, '保存并进入详细设置')}</Button>);
      } else {
        btns.push(<Button
          ecId={`${this && this.props && this.props.ecId || ''}_Button@laztxc@submitAndEnterEdit`}
          type="primary"
          onClick={this.submitAndEnterEdit}
        >{getLabel(32720, '保存并新建')}</Button>);
      }
    }
    btns.push(<Button
      ecId={`${this && this.props && this.props.ecId || ''}_Button@arlnsc@hideDialog`}
      type="primary"
      onClick={() => this.hideDialog()}
    >{getLabel(309, '关闭')}</Button>);
    return btns;
  };

  /**
   * 编辑车辆
   */
  loadEdit = (id) => {
    this.setState({
      itemsVO: true,
      carid: id,
      visible: true,
    });
  };


  open = (data) => {
    const { carInfoStore } = this.props;
    const { form } = carInfoStore;
    this.setState({
      visible: true,
    });
    if (data.subCompanyId == '0') {
      return;
    }
    subid = data.subCompanyId;
    subName = data.subCompanyName;
    form.updateFields({
      subCompanyId: {
        value: data.subCompanyId,
        valueSpan: data.subCompanyName,
        valueObj: [
          {
            id: data.subCompanyId,
            name: data.subCompanyName,
          },
        ],
      },
    });
  };

  back = () => {
    this.refs.carCard.clear();
    this.setState({
      itemsVO: undefined,
      carid: '',
      visible: false,
    });

    if (isRefreshList) { // 刷新列表
      isRefreshList = false;
      const { reloadTableList } = this.props;
      reloadTableList();
    }
  };
  submit = () => {
    isRefreshList = true;
    this.save(this.back.bind(this));
  };
  submitAndSet = () => {
    isRefreshList = true;
    this.save(this.showShare.bind(this));
  };

  showShare = (parm) => {
    this.loadEdit(parm.carid);
    const { carManagerStore } = this.props;
    carManagerStore.getShareList({ id: parm.carid, preview: 0 });
    carManagerStore.setState({
      createOrEdit: 1,
      editSelKey: 1,
      showCreate: true,
      selectId: parm.carid,
    });
    if (isRefreshList) { // 刷新列表
      isRefreshList = false;
      const { reloadTableList } = this.props;
      reloadTableList();
    }
  };

  hideDialog = () => {
    isRefreshList = false;
    this.back();
  };

  submitAndEnterEdit = () => {
    this.save(this.clearData);
  };

  clearData = () => {
    this.refs.carCard.clear();
    isRefreshList = false;
    const { reloadTableList } = this.props;
    reloadTableList();
  };

  save = (callback) => {
    this.refs.carCard.saveCarInfo(callback);
  };
}

export default CarInfoDialog;
